package com.java46.carera.base.exceptionHandler;

import com.java46.carera.base.entity.JsonResult;
import com.java46.carera.base.exception.BusinessException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public JsonResult HandlerException(BusinessException e){
            JsonResult result = new JsonResult();
            result.setMessage(e.getErrorMessage());
            result.setSuccess(false);

            return result;
    }
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ModelAndView HandlerException(Exception e){

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        modelAndView.addObject("errormessage",e.getMessage());
        return modelAndView;
    }
}
